//        var imgnum=0;
var id;
if(/\?id=[0-9a-zA-Z]+/.test(location.href)){
    id=(location.href).match(/id=[0-9a-zA-Z]+/)[0].split("=")[1];
    $.cookie("MYID",id);
}else{
    id= $.cookie("MYID");
}

$("#gobackbtn").click(function(){
    //location.href=document.referrer;
    history.go(-1);
});
//==================================================
//微信部分==========================================
var sendtohref=location.href.match(/qid=[0-9]+/)[0];
//alert($.cookie("qid"));
var qid=sendtohref.match(/[0-9]+/)[0];

//展示大图片------------------------------------------------
function showbigimg(e){
    winscrolltop=$(window).scrollTop();
    //console.log(winscrolltop);
    $("#page_wrapper").css("display","none");
    $("#showimg").attr("src",imgurl+ e.dataset.imgurl);//大图片的地址 和 Id 相同
    $("#digimgplace").css("display","block");
    var bigheight=$("#digimgplace").height();
    var imgheight=$("#showimg").height();
    if(bigheight>imgheight){
        //$("#showimg").css("marginTop",(bigheight-imgheight)/2);
    }
    delimgid=e.dataset.img;//地址 和 Id 相同
}
var winscrolltop;
var imghash={};//保存服务器返回的图片地址 “images.localId[i]”：imgurl
var delimgid="";//要删除的图片的Id
//删除大图
$("#delthisimgbtn").click(function(e){
    imghash[delimgid]="";//清除对应ID 保存的服务器图片地址
    //页面中删掉图片
    var imgbox=document.getElementsByClassName("mainshowimgbox");
    for(var i= 0,r=imgbox.length;i<r;i++){
        if(imgbox[i].dataset.img==delimgid){
            //$imgbox=$(imgbox[i].dataset.img);
            $(".mainshowimgbox")[i].remove();
        }
    }
    $(".modifier.image").css("display","inlineBlock");
    $(".modifier.image").attr("class","modifier image toolbaricon");//还原class
    addimgbtn.style.display="block";
    $("#page_wrapper").css("display","block");
    $("#digimgplace").css("display","none");
});
//关闭地图区域
$("#digimgplace").click(function(e){
    if(e.id!="delthisimgbtn"){
        $("#page_wrapper").css("display","block");
        $("#digimgplace").css("display","none");
        if(winscrolltop>0){
            //console.log(winscrolltop);
            $(window).scrollTop(winscrolltop);
        }
    }
});
//上传图片--------------------------------------------------
//$(".modifier.image").click(function(){
//    console.log($(".modifier.image").attr("class"));
//    if($(".modifier.image").attr("class")==="modifier image toolbaricon"){
//        //if ( typeof(FileReader) === 'undefined' ){
//        //    alert("抱歉，你的浏览器不支持 FileReader，请使用现代浏览器操作！");
//        //} else {
//        //    addimgbtn.addEventListener( 'change',createimg,false );
//        //};
//    }
//});

function createimg(){
    var file = this.files[0];
    console.log(file);
    //判断下类型
    if(!/image\/\w+/.test(file.type)){
        alert("请确保文件为图像类型");
        return false;
    }
    var reader = new FileReader();
    reader.readAsDataURL(file);
    reader.onload = function(e){
        imgresult=this.result;
        $("#sendimginfo").css("display","block");
        $.ajax({
            type: "POST",
            url: "https://www.pulu.com/webapi/api/PuluQuestion/UploadImg",
            data: { "base64":imgresult, "type": ".jpg","flag":2},//flag:1表示头像，2普通照片
            success: function (data) {
                thisimgname=data.Data;
                if(!imghash[thisimgname]){
                    imghash[thisimgname]=thisimgname;
                }
                $("#sendimginfo").css("display","none");
                //$("<div class='mainshowimgbox' style='height:21vw;width:21vw;overflow:hidden;' onclick='showbigimg(this)' data-imgurl='"+thisimgname+"' data-img='"+thisimgname+"'><img src='"+imgurl+thisimgname+"'></div>").insertBefore("#questionbody .modifier.image");
                $("#questionbody").append("<div class='mainshowimgbox' style='height:21vw;width:21vw;overflow:hidden;' onclick='showbigimg(this)' data-imgurl='"+thisimgname+"' data-img='"+thisimgname+"'><img src='"+imgurl+thisimgname+"'></div>");
                if($(".mainshowimgbox").length>=9){
                    //$(imgbtn).css("display","none");
                    $(".modifier.image").attr("class","modifier image toolbaricon cannot");
                    addimgbtn.style.display="none";
                }
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {

            }
        });
    }
}


//点击确定按钮发送回答----------------------------------------------------------------------------------------------
$("#okbtn").click(function(){
    //console.log("点击了");
    var shareuser="";
    if(/shareUserId/.test(location.href)){
        shareuser=(location.href).match(/shareUserId=[0-9a-z]+/)[0].split("=")[1];
    }
    //点击进入
    if($.cookie("shareUserId")){
        shareuser=$.cookie("shareUserId");
    }
    //外部分享进入 级别最高 替换所有
    //if($.cookie("fromuser")){
    //    if($.cookie("fromuser")!=id){
    //        shareuser=$.cookie("fromuser");
    //    }
    //}
    //alert(shareuser);
    if(document.getElementById("contentword").value&&!document.getElementById("contentword").value.match(/^\s+$/g)){
        var answer={};
        answer.questionId=qid;
        answer.answerUserId=id;
        answer.atype=2;  //评论新闻为2
        answer.replyto = "";
        answer.replytoNick = "";
        answer.content=document.getElementById("contentword").value;
        answer.shareUserId=shareuser;
        if(document.getElementById("hidenameinput").checked){
            answer.ishideName=1
        }else{
            answer.ishideName=0;
        }
        //图片
        var imgs=[];
        if(document.getElementsByClassName("mainshowimgbox")&&document.getElementsByClassName("mainshowimgbox").length>0){
            //从数组中获取图片
            var tim=0;
            var len=document.getElementsByClassName("mainshowimgbox").length;
            for(var key in imghash){
                tim++;
                if(imghash[key]){
                    var img={};
                    img=imghash[key];
                    imgs.push(img);
                }
                if(tim==len){
                    $("#sharewaiting").css("display","block");
                    $.ajax({
                        type:"post",
                        datatype:"json",
                        url:"https://www.pulu.com/webapi/api/PuluAnswer/AddAnswer_q",
                        data:{"puluanswer":answer,"imgs":imgs},
                        success:function(data){
                            if(data.Code==="TRUE"){
                                //console.log("回答1");
                                $.cookie("shareUserId","");
                                $.cookie("fromuser","");
                                //经验详情返回
                                if($.cookie("fromquestiondetial")&&$.cookie("fromquestiondetial").length>0){
                                    $.cookie("fromquestiondetial","");
                                    //
                                    $.cookie("reloadPage","reloadPage");
                                    history.go(-1);
                                    //location.href="questiondetail.html?qid="+qid+"#fromuser="+id;
                                }else{
                                    history.go(-1);
                                }
                            }
                        }
                    })
                }
            }
        }else{
            $("#sharewaiting").css("display","block");
            $.ajax({
                type:"post",
                datatype:"json",
                url:"https://www.pulu.com/webapi/api/PuluAnswer/AddAnswer_q",
                data:{"puluanswer":answer,"imgs":imgs},
                success:function(data){
                    if(data.Code==="TRUE"){
                        //console.log("回答2");
                        $.cookie("shareUserId","");
                        $.cookie("fromuser","");
                        //经验详情返回
                        if($.cookie("fromquestiondetial")&&$.cookie("fromquestiondetial").length>0){
                            $.cookie("fromquestiondetial","");
                            //
                            $.cookie("reloadPage","reloadPage");
                            history.go(-1);
                            //location.href="questiondetail.html?qid="+qid+"#fromuser="+id;
                        }else{
                            history.go(-1);
                        }
                    }
                }
            })
        }
    }
    else{
        alert("回答内容不能为空");
    }
});